# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next
class Solution:
    def isPalindrome(self, head: Optional[ListNode]) -> bool:
        tmp1 = tmp2 = head
        while tmp1 and tmp1.next:
            tmp2 = tmp2.next
            tmp1 = tmp1.next.next

        pre, cur = None, tmp2
        while cur:
            nxt = cur.next
            cur.next = pre
            pre = cur
            cur = nxt

        while head != tmp2:
            if head.val != pre.val:
                return False
            head = head.next
            pre = pre.next

        return True